<?php
/**
 * WpDatexServerMt.php
 * 
 * This file contains the WpDatexServerMt class, an extension of WpDatexServer using the 
 * great http://www.mootools.net library for the JavaScript datex adaptions
 * @author Robert Schmidt <robert@wildpack.de>
 * @version 1.0
 * @package wildpack
 * @subpackage dataaccess.wpdatexserver
 */

/**
 * A WpDatexServer implementation for Mootools    
 * 
 * How to setup the server:
 * <code>
 * //wpdserver.php
 * 
 * require_once('path_to_WildPack/WildPack.php');
 * $wp = new WildPack();
 * $wp->using('dataaccess');
 * $wp->using('mvc');
 * $wp->using('web');
 * 
 * //Include all SimpleDatex instances to be used by the server:
 * require_once('Tester.php');
 * require_once('Testers.php');
 * 
 * $l = new WpMemLog();
 * $c = new WpConnectMySQL(array(
 *   'location' => 'localhost/db',
 *   'user' => 'user',
 *   'password' => 'pass',
 *   'log' => $l
 * ));
 * 
 * $req = new WpHttpRequest();
 * $res = new WpHttpResponse();
 * $ses = new WpSimpleSession();
 * $ses->startSession();
 * 
 * $wpds = new WpDatexServerMt($c, $l, $req, $res, $ses, 'wpdserver.php', 'wptest');
 * $wpds->start();
 * </code>
 * 
 * How to access the server:
 * <code>
 * <html>
 * <head><title>WpDatexServerMt test</title></head>
 * <body>
 * 
 * <script type="text/javascript" src="mootools.js"></script>
 * <script type="text/javascript" src="wpdserver.php?js"></script>
 * <script type="text/javascript">
 *    //Now it is possible to access the SimpleDatex classes directly from JS:
 *    window.addEvent('domready', function(){
 *       var t = new wptest.Testers();
 *       t.load([], '', '', 0, 0, function(t, chk){
 *          console.log('Loaded '+String(t.length())+' datasets.');
 *          t.rewind();
 *          while(t.findNext()) {
 *            console.log('Field value: '+t.get('fieldname'));
 *          }
 *       });
 *    });
 * </script>
 * </body>
 * </html>
 * </code>
 * 
 * This class provides functionality to access WpDatex objects from JavaScript. The WpDatex objects 
 * will be available as JavaScript classes (using mootools). The synchronisation with the server 
 * is done by XmlHTTPRequest
 * @author Robert Schmidt <robert@wildpack.de>
 * @version 1.0
 * @package wildpack
 * @subpackage dataaccess.wpdatexserver
 */
class WpDatexServerMt extends WpDatexServer
{
	/**
	 * @see WpDatexServer::getBaseJs()
	 */
	protected function getBaseJs()
	{
		$js = file_get_contents(dirname(__FILE__).'/js/mt/mt_SimpleDatexS_src.js');
		$js .= "\n\n";
		$js .= file_get_contents(dirname(__FILE__).'/js/mt/mt_SimpleDatexM_src.js');
		return $js;
	}
	
	/**
	 * @see WpDatexServer:.getJsForDatexS($name, $instance)
	 */
	protected function getJsForDatexS($name, $instance)
	{
		$js = '//-- Class: '.$name.' --'."\n";
		
		//-- Namespace --
		if ($this->ns !== '') {
			$js .= '//-- Namespace --'."\n";
			$ns = explode('.', $this->ns);
			$path = '';
			foreach ($ns as $nspart) {
				$path .= $nspart;
				$js .= 'if (typeof '.$path.' === \'undefined\') { '.$path.' = {}; }'."\n";
				$path .= '.';
			}
		} else {
			$path = '';
		}
		$js .= "\n";
		
		//-- Get fields and fieldtypes --
		$f = $instance->getFields();
		$ft = '';
		foreach ($f as $fd) {
			$ft .= "\t"."\t"."\t'".$fd.'\': \''.$instance->getFieldType($fd).'\','."\n";
		}
		
		$js .= $path.$name.' = new Class({'."\n".
				"\t".'Implements: [wildpack.SimpleDatexS],'."\n".
				
				"\n".
				"\t".'//-- Constructor --'."\n".
				"\t".'initialize: function() {'."\n".
					"\t"."\t".'this._target = \''.$this->target.'\';'."\n".
					"\t"."\t".'this._class = \''.$name.'\';'."\n".
				"\t".'},'."\n".

				"\n".
				
				"\t".'//-- Retrieves all fieldnames --'."\n".
				"\t".'getFields: function() {'."\n".
					"\t"."\t".'return ['."\n".
						"\t"."\t"."\t'".implode("',\n\t\t\t'", $f)."'\n".
					"\t"."\t".'];'."\n".
				"\t".'},'."\n".
				
				"\t".'//-- Retrieves the fieldtype --'."\n".
				"\t".'getFieldType: function(fieldname) {'."\n".
					"\t"."\t".'var fds = {'."\n".
						$ft."\n".
					"\t"."\t".'};'."\n".
		
					"\t"."\t".'return fds[fieldname];'."\n".
				"\t".'},'."\n".
				
				'});'."\n"."\n";
				
		return $js;
	}
	
	/**
	 * @see WpDatexServer::getJsForDatexM($name, $instance)
	 */
	protected function getJsForDatexM($name, $instance)
	{
		$js = '//-- Class: '.$name.' --'."\n";
		
		//-- Namespace --
		if ($this->ns !== '') {
			$js .= '//-- Namespace --'."\n";
			$ns = explode('.', $this->ns);
			$path = '';
			foreach ($ns as $nspart) {
				$path .= $nspart;
				$js .= 'if (typeof '.$path.' === \'undefined\') { '.$path.' = {}; }'."\n";
				$path .= '.';
			}
		} else {
			$path = '';
		}
		$js .= "\n";
		
		//-- Get fields and fieldtypes --
		$f = $instance->getFields();
		$ft = '';
		foreach ($f as $fd) {
			$ft .= "\t"."\t"."\t'".$fd.'\': \''.$instance->getFieldType($fd).'\','."\n";
		}
		
		$js .= $path.$name.' = new Class({'."\n".
				"\t".'Implements: [wildpack.SimpleDatexM],'."\n".
				
				"\n".
				"\t".'//-- Constructor --'."\n".
				"\t".'initialize: function() {'."\n".
					"\t"."\t".'this._target = \''.$this->target.'\';'."\n".
					"\t"."\t".'this._class = \''.$name.'\';'."\n".
				"\t".'},'."\n".

				"\n".
				
				"\t".'//-- Retrieves all fieldnames --'."\n".
				"\t".'getFields: function() {'."\n".
					"\t"."\t".'return ['."\n".
						"\t"."\t"."\t'".implode("',\n\t\t\t'", $f)."'\n".
					"\t"."\t".'];'."\n".
				"\t".'},'."\n".
				
				"\t".'//-- Retrieves the fieldtype --'."\n".
				"\t".'getFieldType: function(fieldname) {'."\n".
					"\t"."\t".'var fds = {'."\n".
						$ft."\n".
					"\t"."\t".'};'."\n".
		
					"\t"."\t".'return fds[fieldname];'."\n".
				"\t".'},'."\n".
				
				'});'."\n"."\n";
				
		return $js;
	}
}

/* End of file WpDatexServerMt.php */
/* Location: ./wildpack/dataaccess/wpdatexserver/WpDatexServerMt.php */